package com.borrowed.book.weixin.Controller;


import com.alibaba.fastjson.JSONObject;
import com.borrowed.book.book.domain.PayOrderInDto;
import com.borrowed.book.book.factory.PayorderFatory;
import com.borrowed.book.book.service.AbstractPayOrderService;
import com.borrowed.book.system.domain.AjaxResult;
import com.borrowed.book.weixin.entity.vo.CancelPayOrderInDto;
import com.borrowed.book.weixin.entity.vo.CompletePayOrderInDto;
import com.borrowed.book.weixin.service.WxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api(tags = "微信分支付")
@RequestMapping("/api/payScoreOrder")
public class WeixinController {
    @Autowired
    private WxPayService wxPayService;

    @Qualifier("bookPayorderServiceImpl")
    @Autowired
    private AbstractPayOrderService payOrderService;

    @ApiOperation(value = "新增支付订单（微信分）",httpMethod = "POST")
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult AddPayOrderScore(@RequestBody PayOrderInDto indto){
        AjaxResult result;
        try{
            String orderNo=null;
            payOrderService= PayorderFatory.create(indto.getOrderType());
            orderNo=payOrderService.createPayorder(indto);
            JSONObject map= wxPayService.CreatePaysocreOrder(orderNo,indto.getPayAmount());
            result=AjaxResult.sucess("操作成功",map.toString());
        }catch (Exception e){
            e.printStackTrace();
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "支付通知",httpMethod = "POST")
    @PostMapping("/notify")
    @ResponseBody
    @Transactional
    public AjaxResult notify(HttpServletRequest request, HttpServletResponse response){
        try {
            String result=wxPayService.scoreNotifyResult(request, response);
            return AjaxResult.sucess(result);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "取消支付分订单",httpMethod = "POST")
    @PostMapping("/cancle")
    @ResponseBody
    @Transactional
    public AjaxResult CancelPayOrderScore(@RequestBody CancelPayOrderInDto indto){
        AjaxResult result;
        try{
            Boolean resultFlag= wxPayService.CanclePayscoreOrder(indto.getOrderNo(),indto.getReason());
            result=AjaxResult.sucess("操作成功",resultFlag);
        }catch (Exception e){
            e.printStackTrace();
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "完成支付分订单",httpMethod = "POST")
    @PostMapping("/complete")
    @ResponseBody
    @Transactional
    public AjaxResult CompletePayOrderScore(@RequestBody CompletePayOrderInDto indto){
        AjaxResult result;
        try{
            Boolean resultFlag= wxPayService.CompletePayscoreOrder(indto.getOrderNo());
            result=AjaxResult.sucess("操作成功",resultFlag);
        }catch (Exception e){
            e.printStackTrace();
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }
}
